package com.elong.businesstravel.modules.home.hotel.hotelshow;

import java.util.concurrent.TimeUnit;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.CoordinateConversion;
import com.android.support.jhf.utils.MobileUtils;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKMapTouchListener;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.mapapi.map.Symbol;
import com.baidu.mapapi.map.TextItem;
import com.baidu.mapapi.map.TextOverlay;
import com.baidu.mapapi.map.Symbol.Color;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baidusdk.location.BaiduLocation;
import com.elong.businesstravel.base.baidusdk.map.BaiduMapView;
import com.elong.businesstravel.base.baidusdk.map.TouchMapView;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.baseactivity.BaseMapActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.HotelDocs;
import com.elong.businesstravel.protocol.ProtocolDef;

/**
 * 酒店位置
 * 
 * @author lenovo
 * 
 */
public class HotelLocationMapActivity extends BaseMapActivity {

	private MyOverlay mOverlay;
	private PopupOverlay mPopupOverlay;

	private String mGoogleLng;
	private String mGoogleLat;
	private String mHotelName;

	private Bundle mSavedInstanceState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSavedInstanceState = savedInstanceState;
		setContentView(R.layout.activity_hotel_location_map);

		init();
	}

	@Override
	protected void init() {
		if (null == mSavedInstanceState) {
			mGoogleLat = getIntent().getStringExtra("GoogleLat");
			mGoogleLng = getIntent().getStringExtra("GoogleLng");
			mHotelName = getIntent().getStringExtra("HotelName");
		} else {
			mGoogleLat = mSavedInstanceState.getString("GoogleLat");
			mGoogleLng = mSavedInstanceState.getString("GoogleLng");
			mHotelName = mSavedInstanceState.getString("HotelName");
		}

		mMapView = (TouchMapView) findViewById(R.id.mapView);
		initBaiduMap(mMapView);
		mMapView.setBuiltInZoomControls(false);

		// 点击地图上的点弹出的框
		mPopupOverlay = new PopupOverlay(mMapView, null);

		/**
		 * 创建自定义overlay 用来存放显示在地图上的点
		 */
		mOverlay = new MyOverlay(getResources().getDrawable(
				R.drawable.ic_gcoding), mMapView);

		mMapView.refresh();

		/** 将overlay 添加至MapView中 ,只能添加一次 */
		mMapView.getOverlays().add(mOverlay);

		mMapView.regMapViewListener(
				mApplication.mInitBaiduMap.getBMapManager(),
				new MKMapViewListener() {

					@Override
					public void onMapMoveFinish() {
					}

					@Override
					public void onMapLoadFinish() {

						HandlerPostUI.getHandlerPostUI(new Runnable() {
							@Override
							public void run() {
								mShowPopupFlag = true;

								addOverlay(mGoogleLng, mGoogleLat, mHotelName);

								addPopupOverlay(mGoogleLng, mGoogleLat,
										mHotelName);

								mMapController.animateTo(getGeoPoint(
										mGoogleLng, mGoogleLat));

								mMapView.refresh();
							}
						});

					}

					@Override
					public void onMapAnimationFinish() {
						new Thread(new Runnable() {

							@Override
							public void run() {
								try {
									TimeUnit.MILLISECONDS.sleep(250);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								HandlerPostUI.getHandlerPostUI(new Runnable() {

									@Override
									public void run() {
										if (mShowPopupFlag) {
											addPopupOverlay(mGoogleLng,
													mGoogleLat, mHotelName);
										}
									}
								});
							}
						}).start();
					}

					@Override
					public void onGetCurrentMap(Bitmap arg0) {
					}

					@Override
					public void onClickMapPoi(MapPoi arg0) {
					}
				});

		mMapView.regMapTouchListner(new MKMapTouchListener() {

			@Override
			public void onMapLongClick(GeoPoint arg0) {

			}

			@Override
			public void onMapDoubleClick(GeoPoint arg0) {
				// HandlerToastUI.getHandlerToastUI(mContext,
				// "onMapDoubleClick");
				if (mPopupOverlay != null) {
					mPopupOverlay.hidePop();
				}
			}

			@Override
			public void onMapClick(GeoPoint arg0) {

			}
		});

		ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls);
		zoomControls.setOnZoomInClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mPopupOverlay != null) {
					mPopupOverlay.hidePop();
				}
				mMapController.zoomIn();
			}
		});
		zoomControls.setOnZoomOutClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mPopupOverlay != null) {
					mPopupOverlay.hidePop();
				}
				mMapController.zoomOut();
			}
		});

		Button locButton = (Button) findViewById(R.id.locButton);
		locButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				baiduLocation();
			}
		});
		setTitleNavBar();
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("酒店位置");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void addPopupOverlay(String gg_lng, String gg_lat, String hotelName) {

		GeoPoint geoPoint = getGeoPoint(gg_lng, gg_lat);

		View view = LayoutInflater.from(mContext).inflate(
				R.layout.view_hotel_map_name, null);
		TextView hotelNameTextView = (TextView) view
				.findViewById(R.id.hotelNameTextView);
		hotelNameTextView.setText(hotelName);
		hotelNameTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		mPopupOverlay.showPopup(view, geoPoint, 30);
	}

	private GeoPoint getGeoPoint(String gg_lng, String gg_lat) {
		double baiduLat = 0;
		double baiduLng = 0;
		try {
			double googleLat = Double.parseDouble(gg_lat);
			double googleLng = Double.parseDouble(gg_lng);
			double[] lngLat = CoordinateConversion.gg2bd(googleLat, googleLng);
			baiduLat = lngLat[1];
			baiduLng = lngLat[0];
		} catch (Exception e) {
		}
		/**
		 * 准备overlay 数据
		 */
		GeoPoint p1 = new GeoPoint((int) (baiduLat * 1E6),
				(int) (baiduLng * 1E6));

		return p1;
	}

	private void addOverlay(String gg_lng, String gg_lat, String hotelName) {

		GeoPoint p1 = getGeoPoint(gg_lng, gg_lat);

		OverlayItem item1 = new OverlayItem(p1, "", "");
		item1.setTitle(hotelName);
		// item1.setMarker(watermark(R.drawable.ic_marka, price));

		/** 将item 添加到overlay中 注意： 同一个itme只能add一次 */
		mOverlay.addItem(item1);
	}

	public class MyOverlay extends ItemizedOverlay<OverlayItem> {

		public MyOverlay(Drawable defaultMarker, MapView mapView) {
			super(defaultMarker, mapView);
		}

		@Override
		public boolean onTap(int index) {
			mShowPopupFlag = true;
			addPopupOverlay(mGoogleLng, mGoogleLat, mHotelName);
			return true;
		}

		@Override
		public boolean onTap(GeoPoint pt, MapView mMapView) {
			if (mPopupOverlay != null) {
				mShowPopupFlag = false;
				mPopupOverlay.hidePop();
			}
			return false;
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("GoogleLat", mGoogleLat);
		outState.putString("GoogleLng", mGoogleLng);
		outState.putString("HotelName", mHotelName);

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != savedInstanceState) {
			mGoogleLat = savedInstanceState.getString("GoogleLat");
			mGoogleLng = savedInstanceState.getString("GoogleLng");
			mHotelName = savedInstanceState.getString("HotelName");
		}
	}

}
